package com.podium.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.auth.utils.SecurityUtils;
import com.podium.web.system.entity.SysConfig;
import com.podium.web.system.mapper.SysConfigMapper;
import com.podium.web.system.service.SysConfigService;
import com.podium.comment.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 邵文泽
 * @description 参数配置
 * @date 2025-05-09 09:24:07
 */
@Slf4j
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    public IPage<SysConfig> page(Page<SysConfig> page, SysConfig sysConfig) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysConfig::getDelFlag, 0)
                .like(StringUtils.hasLength(sysConfig.getConfigName()), SysConfig::getConfigName, sysConfig.getConfigName())
                .like(StringUtils.hasLength(sysConfig.getConfigKey()), SysConfig::getConfigKey, sysConfig.getConfigKey())
                .orderByDesc(SysConfig::getId)
        ;
        return super.page(page, queryWrapper);
    }

    @Override
    public List<SysConfig> list(SysConfig sysConfig) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysConfig::getDelFlag, 0)
                .like(StringUtils.hasLength(sysConfig.getConfigName()), SysConfig::getConfigName, sysConfig.getConfigName())
                .like(StringUtils.hasLength(sysConfig.getConfigKey()), SysConfig::getConfigKey, sysConfig.getConfigKey())
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysConfig getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysConfig sysConfig) {
        sysConfig.setDelFlag(0L);
        sysConfig.setCreateBy(SecurityUtils.getUsername());
        sysConfig.setCreateTime(LocalDateTime.now());
        return super.save(sysConfig);
    }

    @Override
    public boolean updateById(SysConfig sysConfig) {
        sysConfig.setUpdateBy(SecurityUtils.getUsername());
        sysConfig.setUpdateTime(LocalDateTime.now());
        return super.updateById(sysConfig);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

}